<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Apr 11, 2012
 * Time: 12:08:49 PM
 * To change this template use File | Settings | File Templates.
 */
 
class Login {
    /**
     * Holds instance of database connection
     */
    private $db;

    public function __construct()
    {
        $this->db = new MysqlImprovedDriver();
        $this->db->connect();
    }

// This page defines two functions used by the login/logout process.




/* This function validates the form data (the User Name and password).
 * If both are present, the database is queried.
 * The function requires a database connection.
 * The function returns an array of information, including:
 * - a TRUE/FALSE variable indicating success
 * - an array of either errors or the database result
 */
    public function checkLogin($username = '', $pass = '') {

        $errors = array(); // Initialize error array.

        // Validate the email address:
        if (empty($username)) {
            $errors[] = 'You forgot to enter your email address.';
        } else {
            $un =  $this->db->escape(trim($username));
        }

        // Validate the password:
        if (empty($pass)) {
            $errors[] = 'You forgot to enter your password.';
        } else {
            $p = $this->db->escape(trim($pass));
        }

        if (empty($errors)) { // If everything's OK.

            // Retrieve the user_id and first_name for that email/password combination:
            $q = "SELECT id, user_name,profile_id FROM aea_user WHERE user_name='$un' AND password=SHA1('$p') AND status=1 AND is_delete=0";
            $this->db->prepare($q);
            $this->db->query();
            $r = $this->db->getResult(); // Run the query.

            // Check the result:
            if (mysqli_num_rows($r) == 1) {

                // Fetch the record:               
                $row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

                // Return true and the record:
                return array(true, $row);

            } else { // Not a match!
                $errors[] = 'The user name and password entered do not match those on file.';
            }

        } // End of empty($errors) IF.

        // Return false and the errors:
        return array(false, $errors);

    } // End of check_login() function.

}
